<template>
  <div class="container">
    <nav-bar title="项目规模和类型分布情况"></nav-bar>
    <div class="chart-box chart1">
      <div class="chart-box-header">
        <div class="search">
          <pub-area
            :maxSelectLevel="2"
            @change="changeArea"
            customRightIcon
            label=""
            :isLink="false"
            valueIsLast
            ></pub-area>
          <pub-date @sureDate="sureDate"></pub-date>
        </div>
        <div class="area-detail" @click="goDetailArea"  v-show="useUserStore().orgInfo.level === 1">
          区域分布情况
        </div>
      </div>
      <div class="chart-box-content">
        <div class="bottom-box">
            <div class="left">
              <img src="./img/xdzjs.png" alt="">
              <div class="label">下达资金数（万元）</div>
          </div>
          <div class="value">
              <count-to
                :startVal="0"
                :endVal="state.detail.money"
                :decimals="2"
                :duration="2000"
              ></count-to>
          </div>
        </div>
      </div>
    </div>

    <div class="chart-box chart2">
      <div class="chart-box-header">
        资金来源
      </div>
      <div class="chart-box-content">
        <div class="bottom-box">
          <div class="funds" id="funds"></div>
        </div>
      </div>
    </div>

    <div class="chart-box chart3">
      <div class="chart-box-header">
        资金支出率
      </div>
      <div class="chart-box-content">
        <div class="bottom-box">
          <div class="certificate-item">
            <div class="left">
              <img src="./img/zjzcje.png" alt="">
              <div class="label">
                资金支出金额（万元）
              </div>
            </div>
            <div class="right">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.useMoney)"
                :decimals="2"
                :duration="1500"
              ></count-to>
            </div>
          </div>
          <div class="certificate-item">
            <div class="left">
              <img src="./img/zjjyje.png" alt="">
              <div class="label">
                资金结余金额（万元）
              </div>
            </div>
            <div class="right">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.residueMoney ? state.detail.residueMoney : 0)"
                :decimals="2"
                :duration="1500"
              ></count-to>
            </div>
          </div>
          <div class="certificate-item">
            <div class="left">
              <img src="./img/zjzcl.png" alt="">
              <div class="label">
                资金支出率
              </div>
            </div>
            <div class="right">
              <count-to
                :startVal="0"
                :endVal="Number(state.detail.percent?state.detail.percent:0)"
                :decimals="2"
                :duration="1500"
              ></count-to>%
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useUserStore } from '@/pinia/modules/user'
import dayjs from "dayjs"
import { CountTo } from 'vue3-count-to'
import * as echarts from 'echarts'
import { ref, reactive, onBeforeMount,onMounted,nextTick,onBeforeUnmount } from 'vue'
import { get_fund_analysis } from '@/api/elderDistribution'
import { appCurrentState } from '@/pinia/modules/app.js'
import { formatDictCode } from '@/utils/format.js'
import { useRouter } from 'vue-router'
const active = ref(0);
const router = useRouter()
const state = reactive({
  searchParams: {
    areaCode: useUserStore().userLastArea,
    year:dayjs().format('YYYY')
  },
  detail: {},
  myChart1:null
})
const getElderDetail = async () => {
  let res = await get_fund_analysis(state.searchParams)
  if (res.succeed) {
    state.detail = res.data
    nextTick(() => {
      initChartFunds()
    })
  }
}

const changeArea = val => {
  state.searchParams.areaCode = val
  getElderDetail()
}


const sureDate = val => {
  state.searchParams.year = val[0]
  getElderDetail()
}

const  initChartFunds = async () => {
  state.myChart1 = echarts.init(document.getElementById('funds'))
  const data = [
          { value: state.detail.source1, name: '中央财政' },
          { value: state.detail.source2, name: '省财政' }

        ]
  const option = {
    legend: {
      top: 'bottom',
      right: '0%',
      orient  : 'vertical'
    },
    toolbox: {
      show: false,
    },

      tooltip: {
      trigger: 'item',
      formatter: '{b}: {c}万元',
      backgroundColor: 'rgba(47,37,198,0.75)',
      borderColor :'#2880FF',
        borderWidth: 1,
        textStyle:{
          color  :'#fff'
        }
    },
    series: [
      {
        name: 'Nightingale Chart',
        type: 'pie',
        radius: ['30%','80%'],
        center: ['50%', '50%'],
        roseType: 'area',
        label: {
          show: false,
          formatter: '{b}:{d}%',
        },
        labelLine: {
          length: 1, //第一条线
          length: 2, //第二条线
          lineStyle: {
              width: 1, // 线条的宽度
          }
        },
        itemStyle: {
          borderColor: '#fff',
          borderWidth: 2
        },
        data: data
      }
    ]
  };

  state.myChart1 .setOption(option)
}


const goDetailArea = () => {
  router.push('/fundArea')
}

const changeTab = val => {
}
onBeforeUnmount(() => {
  state.myChart1 && state.myChart1.dispose()
})

onMounted(() => {
  getElderDetail()
})
</script>
<style lang="less" scoped>
* {
  box-sizing: border-box;
}
.container {
  height: 100%;
  background-color: #f8f8f8;
  overflow-y: auto;

  .chart-box {
    // width: calc(100% - 80px);
    width: 100%;
    font-size: 30px;
    font-family: Microsoft YaHei;
    color: #000000;
    margin-top: 20px;
    background-color: #fff;

    .chart-box-header {
      display: flex;
      align-items: center;
      height: 90px;
      border-bottom: 1px solid #E6E6E6;
    }
  }

  .chart1 {
    height: 250px;

    .chart-box-header {
      justify-content: space-between;
      :deep(.van-cell) {
          width: 220px;
          height: 100%;
        }
      .search {
        display: flex;
        align-items: center;
      }
      .area-detail {
        padding-right: 40px;
        font-weight: 400;
        color: #4375E4;
      }
    }

    .chart-box-content {
      .bottom-box {
        height: 140px;
        width: 100%;
        display: flex;
        padding: 0 40px;
        justify-content:space-between;
        align-items: center;
        font-size: 24px;
        font-family: PingFang SC;
        font-weight: 500;
        color: #333333;

        .left {
          display: flex;
          align-items: center;
        }

        .value{
          font-size: 36px;
        }

        img {
          width: 97px;
          height: 97px;
          margin-right: 20px;
        }
      }
    }
  }

  .chart2 {
    height: 440px;
    .chart-box-header {
      padding-left: 40px;
      font-weight: bold;
    }

      .bottom-box {
        padding: 0 40px;
        .funds {
          width: 100%;
          height: 350px;
        }
      }
  }

  .chart3 {
    height: 430px;
    .chart-box-header {
      padding-left: 40px;
      font-weight: bold;
    }

      .bottom-box {
        padding: 0 40px;
        .certificate-item {
          height: 113px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 24px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #000000;
          border-bottom: 1px dashed #959595;

          .left {
            display: flex;
            align-items: center;
            img {
              width: 51px;
              height: 51px;
              margin-right: 25px;
            }
          }

          .right {
            font-size: 36px;
          }
        }

        .certificate-item:nth-child(3){
          border-bottom: none;
        }
      }
  }
}
</style>
